package tree_util;

import com.common.RequestDataTool;
import com.opensymphony.xwork2.ActionSupport;
import db_tool.DbBean;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

public class ShowRequestData extends ActionSupport {

    @Override
    public String execute() {

        DbBean db = new DbBean();
        HttpSession session = ServletActionContext.getRequest().getSession();
        Account account = (Account) session.getAttribute("account");
        ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
        RequestDataTool tool = (RequestDataTool) context.getBean("requestDataTool");
        String status = tool.isRequesting(account.getUsername());
        if ("agree".equals(status)) {
            Map<String, Map<String, List<String>>> map = db.executeGetRequestData(account.getUsername());
            session.setAttribute("allowData", map);
            if(map.size() > 0){
                return SUCCESS;
            }else {
                session.setAttribute("error", "noDatas");
                return ERROR;
            }
        } else {
            session.setAttribute("error", status);
            return ERROR;
        }
    }

}
